package com.czk.rocketmq.batch;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 发送批量消息
 *
 * @Author:ChenZhangKun
 * @Date: 2020/9/6 16:46
 */
@SuppressWarnings("all")
public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        // 1.创建消息生产者producer，并制定生产者组名
        DefaultMQProducer producer = new DefaultMQProducer("group1");
        // 2.指定Nameserver地址
        producer.setNamesrvAddr("192.168.25.133:9876;192.168.25.134:9876");
        // 3.启动producer
        producer.start();
        // 发送批量消息
        List<Message> list = new ArrayList<>();
        // 4.创建消息对象，指定主题Topic、Tag和消息体
        Message message = new Message("batchTopic", "tag1", "czk".getBytes());
        Message message1 = new Message("batchTopic", "tag1", "czj".getBytes());
        Message message2 = new Message("batchTopic", "tag1", "czh".getBytes());
        list.add(message);
        list.add(message1);
        list.add(message2);
        producer.send(list);
        TimeUnit.SECONDS.sleep(1);
    }
}


